<?php

/**
 * dashboard home control
 * @author wangXi <iwisunny@gmail.com>
 * Date: 2015/12/22 10:42
 */
class Home extends Home_Controller
{
    protected $ajax_op='';

    const INET_RETRY_TIME = 20; //网络超时判断阈值

    function __construct()
    {
        parent::__construct();

        $this->ajax_op=$this->input->post_get('op',true);
    }

    function index(){
        $this->tpl->render('index.twig', []);
    }

    function ajax_handle(){
        if(!is_xhr()){
            $this->put_msg();
        }

        //获取所有租赁机状态
        if($this->ajax_op=='get_kiosk_status'){
            //delay load model to optimize page response
            $this->load->model('kiosk_status_model','kiosk_status');

            $kiosk_status=array();
            $all_kiosk=$this->kiosk_status->get_all();

            if($all_kiosk){
                foreach($all_kiosk as $num => &$item){
                    $item['last_sync_time']=date('Y-m-d H:i:s', $item['last_sync_time']);
                    $item['last_boot_time']=date('Y-m-d H:i:s', $item['last_boot_time']);

                    //根据update_time的时间间隔初步判断租赁机是否联网，阈值=20s
                    $item['connected']=time()-intval($item['update_time']) > self::INET_RETRY_TIME ? 0 : 1;
                }

                $kiosk_status=$all_kiosk;
            }

            $this->put_msg(array('result'=>$kiosk_status, 'success'=>true));

        }

        //从unibox_rental库获取租赁机数据
        if($this->ajax_op == 'get_rental_kiosk_list'){
            //use another db connection
            $DB_rental=$this->load->database('rental', true);
            $query_all=$DB_rental->select('kiosk_id, address')->get('kiosk');

            $this->set_msg(array(
                'result'=>$query_all ? $query_all->result_array() : array(),
                'success'=>true
            ));
        }

        if($this->ajax_op=='op_teamvwr'){
            $this->load->model('kiosk_addon_model', 'kiosk_addon');
            $kiosk_id=intval($this->input->post_get('kiosk_id',true));

            $fm_data=$this->input->post_get('data',true);

            if(!empty($fm_data)){
//                if(empty($fm_data['teamv_id']) || empty($fm_data['teamv_pwd'])){
//                    $this->put_msg(['info'=>'id or password empty']);
//                }

                $status=$this->kiosk_addon->set_data($kiosk_id, [
                    'teamvwr_id'=> trim($fm_data['teamv_id']),
                    'teamvwr_pwd'=> trim($fm_data['teamv_pwd'])
                ]);
            }
            else{
                //fetch row
                $row=$this->kiosk_addon->get_by(['kiosk_id'=>$kiosk_id]);
                $this->put_msg(['result'=>$row ? $row : [], 'success'=>true]);
            }

            $this->set_msg(['success'=>$status]);
        }

        if($this->ajax_op=='get_teamvwr_list'){
            $this->load->model('kiosk_addon_model', 'kiosk_addon');
            $teamvwr_list=$this->kiosk_addon->get_filled_teamvwr();
            $this->set_msg(['result'=>$teamvwr_list]);

        }

        if($this->ajax_op=='op_memo_log'){
            $this->load->model('kiosk_memo_log_model', 'memo_log');
            $kiosk_id=intval($this->input->post_get('kiosk_id',true));

            $data=$this->input->post_get('data',true);
            if(!empty($data)){
                $log_msg=trim($data['log_msg']);
                $ins_id=$this->memo_log->insert([
                    'kiosk_id'=>$kiosk_id,
                    'log_msg'=> $log_msg,
                    'log_time'=>time(),
                    'log_author'=> $this->session->userdata('username')
                ]);
                $this->put_msg(['success'=>$ins_id>0]);
            }
            else{
                //fetch memo log
                $rows=$this->memo_log->get_memo_list(['kiosk_id'=>$kiosk_id]);
                foreach($rows as &$r){
                    $r['log_msg']=urldecode($r['log_msg']);
                    $r['log_time']=date('Y-m-d H:i:s', $r['log_time']);
                }

                $this->put_msg([
                    'result'=>$rows ? $rows : [],
                    'success'=>true
                ]);
            }

        }

        $this->put_msg();

    }

}
